from selenium.webdriver.common.by import By

from page_object.base_page import BasePage
from page_object.commodity_info import CommodityInfoPage
from page_object.commodity_page import CommodityClassPage

from utils.logger import logger


class JDHomePage(BasePage):
    _BASE_URL = "https://www.jd.com/"
    _COMPUTER_CLASS = (By.XPATH, "//a[text()='电脑' and @class='cate_menu_lk']")
    _NOTEBOOK = (By.XPATH, "//a[text()='笔记本' and @class='cate_detail_con_lk']")
    _PANTS_CLASS = (By.XPATH, "//a[text()='男装' and @class='cate_menu_lk']")
    _JEANS = (By.XPATH, "//a[text()='牛仔裤' and @class='cate_detail_con_lk']")
    _SHOES_CLASS = (By.XPATH, "//a[text()='女鞋' and @class='cate_menu_lk']")
    _MADING = (By.XPATH, "//a[text()='马丁鞋' and @class='cate_detail_con_lk']")
    _APPLE_CLASS = (By.XPATH, "//a[text()='生鲜' and @class='cate_menu_lk']")
    _APPLE = (By.XPATH, "//a[text()='苹果' and @class='cate_detail_con_lk']")
    _BOOK_CLASS = (By.XPATH, "//a[text()='图书' and @class='cate_menu_lk']")
    _COMPUTER_BOOK = (By.XPATH, "//a[text()='计算机与互联网' and @class='cate_detail_con_lk']")
    _FURNITURE_CLASS = (By.XPATH, "//a[text()='家具' and @class='cate_menu_lk']")
    _SOFA = (By.XPATH, "//a[text()='沙发' and @class='cate_detail_con_lk']")

    def goto_computer_page(self):
        """点击前往电脑分类"""
        logger.info("点击前往电脑分类")
        self.move_to_element_action(self._COMPUTER_CLASS)
        self.find_and_click(self._NOTEBOOK)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_jeans_page(self):
        """点击前往牛仔裤分类"""
        logger.info("点击前往牛仔裤分类")
        self.move_to_element_action(self._PANTS_CLASS)
        self.find_and_click(self._JEANS)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_mading_shoes_page(self):
        """点击前往马丁鞋分类"""
        logger.info("点击前往马丁鞋分类")
        self.move_to_element_action(self._SHOES_CLASS)
        self.find_and_click(self._MADING)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_apple_page(self):
        """点击前往苹果分类"""
        logger.info("点击前往苹果分类")
        self.move_to_element_action(self._APPLE_CLASS)
        self.find_and_click(self._APPLE)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_computer_book_page(self):
        """点击前往计算机图书分类"""
        logger.info("点击前往计算机图书分类")
        self.move_to_element_action(self._BOOK_CLASS)
        self.find_and_click(self._COMPUTER_BOOK)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_sofa_page(self):
        """点击前往沙发分类"""
        logger.info("点击前往沙发分类")
        self.move_to_element_action(self._FURNITURE_CLASS)
        self.find_and_click(self._SOFA)
        self.switch_to_next_windows()
        return CommodityClassPage(self.driver)

    def goto_commodity_url(self, url):
        """切换到最后一个窗口"""
        logger.info(f"访问{url}")
        self.driver.get(url)
        return CommodityInfoPage(self.driver)
